<template>
  <div class="app-container">
    <rule-card :form="form" @edit-rule="handleEditRule" />
    <rule-dialog
      :visible="ruleOpen"
      :form="form"
      @update:visible="ruleOpen = $event"
      @rule-updated="handleRuleUpdated"
    />
    <trigger-card
      :trigger-form="triggerForm"
      :term-list="termList"
      :action-list="actionList"
      @add-trigger="handleAddTrigger"
      @edit-trigger="handleEditTrigger"
      @delete-trigger="handleDeleteTrigger"
      @add-term="handleAddTerm"
      @edit-term="handleEditTerm"
      @delete-term="handleDeleteTerm"
      @add-action="handleAddAction"
      @edit-action="handleEditAction"
      @delete-action="handleDeleteAction"
    />
    <trigger-dialog
      :visible="triggerOpen"
      :trigger-form="triggerForm"
      :rule-id="form.ruleId"
      @update:visible="triggerOpen = $event"
      @trigger-updated="handleTriggerUpdated"
    />
    <term-dialog
      :visible="termOpen"
      :trigger-id="triggerForm.triggerId"
      :term="currentTerm"
      :title="termTitle"
      @update:visible="termOpen = $event"
      @term-updated="getTermList"
    />
    <action-dialog
      :visible="actionOpen"
      :rule-id="form.ruleId"
      :action="currentAction"
      :title="actionTitle"
      @update:visible="actionOpen = $event"
      @action-updated="getActionList"
    />
  </div>
</template>

<style scoped>
.app-container {
  padding: 20px;
}
</style>

<script>
import RuleCard from "./ruleCard.vue";
import RuleDialog from "./ruleDialog.vue";
import TriggerCard from "./triggerCard.vue";
import TriggerDialog from "./triggerDialog.vue";
import TermDialog from "./termDialog.vue";
import ActionDialog from "./actionDialog.vue";
import { getRule } from "@/api/system/iot/rule";
import { getTriggerByRule, delTrigger } from "@/api/system/iot/trigger";
import { listTerm, delTerm } from "@/api/system/iot/term";
import {
  selectIotRulesActionList,
  deleteIotRulesActionByActionIds,
} from "@/api/system/iot/action";

export default {
  components: {
    RuleCard,
    RuleDialog,
    TriggerCard,
    TriggerDialog,
    TermDialog,
    ActionDialog,
  },
  data() {
    return {
      ruleOpen: false,
      triggerOpen: false,
      termOpen: false,
      actionOpen: false,
      termTitle: "",
      actionTitle: "",
      form: {},
      triggerForm: {
        ruleId: "",
        remark: "",
        triggerCondition: "or",
        isRestrain: "0",
        dataTime: 0,
        repeatNum: 0,
        intervalTime: 0,
        termType: "",
      },
      termList: [],
      actionList: [],
      currentTerm: {},
      currentAction: {},
    };
  },
  created() {
    const ruleId = this.$route.params.ruleId;
    this.getRuleDetail(ruleId);
  },
  methods: {
    getRuleDetail(ruleId) {
      getRule(ruleId).then((response) => {
        this.form = response.data;
        if (response.data.ruleId) {
          this.getTriggersInfo(response.data.ruleId);
          this.getActionList(response.data.ruleId);
        }
      });
    },
    getTriggersInfo(ruleId) {
      getTriggerByRule(ruleId).then((response) => {
        this.triggerForm = response.data;
        if (this.triggerForm.triggerId) {
          this.getTermList(this.triggerForm.triggerId);
        }
      });
    },
    getTermList(triggerId) {
      listTerm({ triggerId: triggerId }).then((response) => {
        this.termList = response.rows;
      });
    },
    getActionList(ruleId) {
      selectIotRulesActionList({ ruleId: ruleId }).then((response) => {
        this.actionList = response.rows;
      });
    },
    handleEditRule() {
      this.ruleOpen = true;
    },
    handleRuleUpdated(rule) {
      this.form = { ...rule };
    },
    handleAddTrigger() {
      this.triggerForm = {
        ruleId: this.form.ruleId,
        remark: "",
        triggerCondition: "or",
        isRestrain: "0",
        dataTime: 0,
        repeatNum: 1,
        intervalTime: 0,
        termType: "",
      };
      this.triggerOpen = true;
    },
    handleEditTrigger() {
      this.triggerOpen = true;
    },
    handleDeleteTrigger() {
      this.$modal
        .confirm("是否确认删除该触发器？")
        .then(() => {
          delTrigger(this.triggerForm.triggerId).then(() => {
            this.$modal.msgSuccess("删除成功");
            this.triggerForm = {
              ruleId: this.form.ruleId,
              remark: "",
              triggerCondition: "or",
              isRestrain: "0",
              dataTime: 0,
              repeatNum: 0,
              intervalTime: 0,
              termType: "",
            };
            this.termList = [];
            this.actionList = [];
          });
        })
        .catch(() => {});
    },
    handleTriggerUpdated(trigger) {
      this.triggerForm = { ...trigger };
      if (trigger.triggerId) {
        this.getTermList(trigger.triggerId);
      }
    },
    handleAddTerm() {
      this.currentTerm = {};
      this.termTitle = "添加触发项";
      this.termOpen = true;
    },
    handleEditTerm(term) {
      this.currentTerm = term;
      this.termTitle = "修改触发项";
      this.termOpen = true;
    },
    handleDeleteTerm(term) {
      this.$modal
        .confirm('是否确认删除触发项编号为"' + term.termId + '"的数据项？')
        .then(() => {
          delTerm(term.termId).then(() => {
            this.$modal.msgSuccess("删除成功");
            this.getTermList(this.triggerForm.triggerId);
          });
        })
        .catch(() => {});
    },
    handleAddAction() {
      this.currentAction = {};
      this.actionTitle = "添加触发动作";
      this.actionOpen = true;
    },
    handleEditAction(action) {
      this.currentAction = action;
      this.actionTitle = "修改触发动作";
      this.actionOpen = true;
    },
    handleDeleteAction(action) {
      this.$modal
        .confirm(
          '是否确认删除触发动作编号为"' + action.actionId + '"的数据项？'
        )
        .then(() => {
          deleteIotRulesActionByActionIds([action.actionId]).then(() => {
            this.$modal.msgSuccess("删除成功");
            this.getActionList(this.form.ruleId);
          });
        })
        .catch(() => {});
    },
  },
};
</script>
